/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.main;

import com.seari.bean.BaseClient;
import com.seari.bean.IPandPortBean;
import com.seari.processor.ChannelChecker;
import com.seari.processor.ChannelExchanger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Rainbow
 */
public class NettyClientMain
{

    protected static Logger logger = LoggerFactory.getLogger(NettyClientMain.class);

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, Exception
    {
        //args = new String[]{"com.seari.pa.PaClient","127.0.0.1","4001"};
        BaseClient client = null;
        if (null != args && args.length > 0)
        {
            client = (BaseClient) Class.forName(args[0]).newInstance();
            if (null != client)
            {
                if (args.length < 4)
                {
                    client.setHost(args[1]);
                    client.setPort(Integer.parseInt(args[2]));
                    client.setup();
                    ChannelChecker checker = new ChannelChecker(client);
                    Thread thread = new Thread(checker);
                    thread.start();
                } else
                {
                    IPandPortBean masterIPandPortBean = new IPandPortBean(args[1],Integer.parseInt(args[2]));
                    IPandPortBean slaveIPandPortBean = new IPandPortBean(args[3],Integer.parseInt(args[4]));
                    client.setHost(masterIPandPortBean.getIp());
                    client.setPort(masterIPandPortBean.getPort());
                    client.setup();
                    ChannelExchanger exchanger = new ChannelExchanger(client);
                    exchanger.setCurrentServer(ChannelExchanger.MASTER);
                    exchanger.getExchangeMap().put(ChannelExchanger.MASTER, masterIPandPortBean);
                    exchanger.getExchangeMap().put(ChannelExchanger.SLAVE, slaveIPandPortBean);
                    Thread thread = new Thread(exchanger);
                    thread.start();
                }
            }
        } else
        {
            logger.warn("args missing");
        }

    }
}
